<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="爱肖彤真是太好了"><meta name="copyright" content="爱肖彤真是太好了"><meta name="generator" content="Hexo 5.1.1"><meta name="theme" content="hexo-theme-yun"><title>drf☞jwt自动签发与手动签发 | 工藤旧二の博客</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="none" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.19/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_stqaphw3j4.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/task/yun.ico"><link rel="mask-icon" href="/task/yun.ico" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/task/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/task/js/utils.js" as="script"><link rel="preload" href="/task/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/task/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/task/css/hexo-theme-yun.css"><link rel="alternate" href="/task/atom.xml" title="工藤旧二の博客" type="application/atom+xml"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"root":"/task/","title":"工藤旧二の博客","version":"0.9.7","anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"local_search":{"path":"/task/search.xml"},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><script>(function(){
  var bp = document.createElement('script');
  var curProtocol = window.location.protocol.split(':')[0];
  if (curProtocol === 'https') {
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else {
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(bp, s);
})();</script><meta name="description" content="一、自动签发urls from rest_framework_jwt.views import obtain_jwt_token # 使用jwt自带的登录视图 urlpatterns &#x3D; [     path(&#39;login&#x2F;&#39;, obtain_jwt_token), ] settings import datetime JWT_AUTH&#x3D;{     # 配置响应格式，必须和自动签发使用   &#39;JW">
<meta property="og:type" content="article">
<meta property="og:title" content="drf☞jwt自动签发与手动签发">
<meta property="og:url" content="http://huang_zhao.gitee.io/task/2020/02/15/python/%E6%A1%86%E6%9E%B6/drf%E2%98%9Ejwt%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%8F%91%E4%B8%8E%E6%89%8B%E5%8A%A8%E7%AD%BE%E5%8F%91/index.html">
<meta property="og:site_name" content="工藤旧二の博客">
<meta property="og:description" content="一、自动签发urls from rest_framework_jwt.views import obtain_jwt_token # 使用jwt自带的登录视图 urlpatterns &#x3D; [     path(&#39;login&#x2F;&#39;, obtain_jwt_token), ] settings import datetime JWT_AUTH&#x3D;{     # 配置响应格式，必须和自动签发使用   &#39;JW">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-02-15T07:33:45.000Z">
<meta property="article:modified_time" content="2020-08-26T00:59:07.000Z">
<meta property="article:author" content="爱肖彤真是太好了">
<meta property="article:tag" content="drf">
<meta property="article:tag" content="jwt">
<meta name="twitter:card" content="summary"><script src="/task/js/ui/mode.js"></script><link rel="stylesheet" href="/task/css/prism.css" type="text/css"></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/task/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/task/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="文章目录"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="站点概览"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/task/about/" title="爱肖彤真是太好了"><img width="96" loading="lazy" src="/task/Yun.png" alt="爱肖彤真是太好了"></a><div class="site-author-name"><a href="/task/about/">爱肖彤真是太好了</a></div><a class="site-name" href="/task/about/site.html">工藤旧二の博客</a><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/task/" title="首页"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/task/archives/" title="归档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">57</span></a></div><div class="site-state-item"><a href="/task/categories/" title="分类"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">2</span></a></div><div class="site-state-item"><a href="/task/tags/" title="标签"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">50</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=552858287" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/15081363" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-send-plane-2-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/task/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%8F%91"><span class="toc-text">一、自动签发</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E6%89%8B%E5%8A%A8%E7%AD%BE%E5%8F%91"><span class="toc-text">二、手动签发</span></a></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://huang_zhao.gitee.io/task/task/2020/02/15/python/%E6%A1%86%E6%9E%B6/drf%E2%98%9Ejwt%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%8F%91%E4%B8%8E%E6%89%8B%E5%8A%A8%E7%AD%BE%E5%8F%91/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="爱肖彤真是太好了"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="工藤旧二の博客"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">drf☞jwt自动签发与手动签发</h1><div class="post-meta"><div class="post-time" style="display:block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="创建时间：2020-02-15 15:33:45" itemprop="dateCreated datePublished" datetime="2020-02-15T15:33:45+08:00">2020-02-15</time><span class="post-meta-divider">-</span><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-2-line"></use></svg></span> <time title="修改时间：2020-08-26 08:59:07" itemprop="dateModified" datetime="2020-08-26T08:59:07+08:00">2020-08-26</time></div><span class="post-count"><span class="post-symbolcount"><span class="post-meta-item-icon" title="本文字数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-file-word-line"></use></svg></span> <span title="本文字数">969</span><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读时长"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-timer-line"></use></svg></span> <span title="阅读时长">4m</span></span></span><span class="post-busuanzi"><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读次数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg> <span id="busuanzi_value_page_pv"></span></span></span><div class="post-classify"><span class="post-category"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span> <span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/task/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">技术分享</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/task/tags/drf/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">drf</span></a><a class="tag" href="/task/tags/jwt/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">jwt</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h2 id="一、自动签发"><a href="#一、自动签发" class="headerlink" title="一、自动签发"></a>一、自动签发</h2><p><strong>urls</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">from</span> rest_framework_jwt<span class="token punctuation">.</span>views <span class="token keyword">import</span> obtain_jwt_token
<span class="token comment" spellcheck="true"># 使用jwt自带的登录视图</span>
urlpatterns <span class="token operator">=</span> <span class="token punctuation">[</span>
    path<span class="token punctuation">(</span><span class="token string">'login/'</span><span class="token punctuation">,</span> obtain_jwt_token<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span></code></pre>
<p><strong>settings</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> datetime
JWT_AUTH<span class="token operator">=</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true"># 配置响应格式，必须和自动签发使用</span>
  <span class="token string">'JWT_RESPONSE_PAYLOAD_HANDLER'</span><span class="token punctuation">:</span><span class="token string">'app01.utils.my_jwt_response_payload_handler'</span><span class="token punctuation">,</span>
    <span class="token string">'JWT_EXPIRATION_DELTA'</span><span class="token punctuation">:</span> datetime<span class="token punctuation">.</span>timedelta<span class="token punctuation">(</span>days<span class="token operator">=</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true"># 过期时间，手动配置</span>
<span class="token punctuation">}</span></code></pre>
<p><strong>utils</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 重写jwt响应格式（需要到settings配置）</span>
<span class="token comment" spellcheck="true"># 与之配合使用的必须是自动签发</span>
<span class="token keyword">def</span> <span class="token function">my_jwt_response_payload_handler</span><span class="token punctuation">(</span>token<span class="token punctuation">,</span> user<span class="token operator">=</span>None<span class="token punctuation">,</span> request<span class="token operator">=</span>None<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 返回什么，前端就能看到什么样子</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
        <span class="token string">'token'</span><span class="token punctuation">:</span> token<span class="token punctuation">,</span>
        <span class="token string">'msg'</span><span class="token punctuation">:</span><span class="token string">'登录成功'</span><span class="token punctuation">,</span>
        <span class="token string">'status'</span><span class="token punctuation">:</span><span class="token number">100</span><span class="token punctuation">,</span>
        <span class="token string">'username'</span><span class="token punctuation">:</span>user<span class="token punctuation">.</span>username

    <span class="token punctuation">}</span></code></pre>
<p>然后直接在前端提交post请求发送账号和密码，会返回我们定义好的响应格式</p>
<pre class=" language-python"><code class="language-python"><span class="token punctuation">{</span>
    <span class="token string">"token"</span><span class="token punctuation">:</span> <span class="token string">"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6Imh6IiwiZXhwIjoxNTk1NDE1MTEyLCJlbWFpbCI6IiJ9.BxBdsm6oBz8iPAwSSpo_7IaU4pBp6RjK4c0GJ_FYN1E"</span><span class="token punctuation">,</span>
    <span class="token string">"msg"</span><span class="token punctuation">:</span> <span class="token string">"登录成功"</span><span class="token punctuation">,</span>
    <span class="token string">"status"</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">,</span>
    <span class="token string">"username"</span><span class="token punctuation">:</span> <span class="token string">"hz"</span>
<span class="token punctuation">}</span></code></pre>
<p>然后拿出token对测试类发送测试请求</p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">TestAPI</span><span class="token punctuation">(</span>APIView<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">get</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>request<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span>user<span class="token punctuation">)</span>
        <span class="token keyword">return</span> Response<span class="token punctuation">(</span><span class="token string">"ok"</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 因为内置的他没有对匿名用户设置拦截，素以匿名用户也能看到ok</span>
<span class="token comment" spellcheck="true"># 我们用request.user来区分</span>
<span class="token comment" spellcheck="true"># 这里可能会出现我登录了很多次，用每次不同的token都能登录</span>
<span class="token comment" spellcheck="true"># 这是因为token校验的是规则，是要加密规则符合且没有超时，那用哪次token都一样的</span></code></pre>
<h2 id="二、手动签发"><a href="#二、手动签发" class="headerlink" title="二、手动签发"></a>二、手动签发</h2><p><strong>utils</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">from</span> rest_framework_jwt<span class="token punctuation">.</span>authentication <span class="token keyword">import</span> BaseJSONWebTokenAuthentication<span class="token punctuation">,</span>jwt_decode_handler
<span class="token keyword">import</span> jwt
<span class="token keyword">from</span> rest_framework<span class="token punctuation">.</span>exceptions <span class="token keyword">import</span> AuthenticationFailed
<span class="token keyword">class</span> <span class="token class-name">MyAuthentication</span><span class="token punctuation">(</span>BaseJSONWebTokenAuthentication<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># 这里重写的逻辑和BaseJSONWebTokenAuthentication里的authenticate一模一样</span>
    <span class="token keyword">def</span> <span class="token function">authenticate</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> request<span class="token punctuation">)</span><span class="token punctuation">:</span>
        jwt_token <span class="token operator">=</span> request<span class="token punctuation">.</span>META<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'HTTP_AUTHORIZATION'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 获取浏览器传来的token</span>
        <span class="token keyword">if</span> jwt_token<span class="token punctuation">:</span>
            <span class="token keyword">try</span><span class="token punctuation">:</span>
                payload <span class="token operator">=</span> jwt_decode_handler<span class="token punctuation">(</span>jwt_token<span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 传入token，拿出第二段用户信息,有内置的校验token功能</span>
            <span class="token keyword">except</span> jwt<span class="token punctuation">.</span>ExpiredSignature<span class="token punctuation">:</span>
                <span class="token keyword">raise</span> AuthenticationFailed<span class="token punctuation">(</span><span class="token string">'签名过期'</span><span class="token punctuation">)</span>
            <span class="token keyword">except</span> jwt<span class="token punctuation">.</span>InvalidTokenError<span class="token punctuation">:</span>
                <span class="token keyword">raise</span> AuthenticationFailed<span class="token punctuation">(</span><span class="token string">'用户非法'</span><span class="token punctuation">)</span>
            <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
                <span class="token comment" spellcheck="true"># 所有异常都会走到这</span>
                <span class="token keyword">raise</span> AuthenticationFailed<span class="token punctuation">(</span>str<span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># 通过内置的方法把payload转换成用户对象</span>
            user <span class="token operator">=</span> self<span class="token punctuation">.</span>authenticate_credentials<span class="token punctuation">(</span>payload<span class="token punctuation">)</span>
            <span class="token keyword">return</span> user<span class="token punctuation">,</span>None <span class="token comment" spellcheck="true"># ===》request.user,request.auth</span>
        <span class="token keyword">raise</span> AuthenticationFailed<span class="token punctuation">(</span><span class="token string">'您没有携带认证信息'</span><span class="token punctuation">)</span></code></pre>
<p><strong>sers</strong></p>
<pre class=" language-python"><code class="language-python">
<span class="token keyword">from</span> rest_framework <span class="token keyword">import</span> serializers


<span class="token comment" spellcheck="true"># 多方序列化校验登录</span>
<span class="token keyword">import</span> re
<span class="token keyword">from</span> rest_framework<span class="token punctuation">.</span>exceptions <span class="token keyword">import</span> ValidationError
<span class="token keyword">from</span> app01 <span class="token keyword">import</span> models
<span class="token keyword">from</span> rest_framework_jwt<span class="token punctuation">.</span>utils <span class="token keyword">import</span> jwt_encode_handler<span class="token punctuation">,</span>jwt_payload_handler
<span class="token keyword">class</span> <span class="token class-name">LoginSer</span><span class="token punctuation">(</span>serializers<span class="token punctuation">.</span>ModelSerializer<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># 我们要提交校验数据的时候，如果直接用下面Meta绑定给模型类的话</span>
    <span class="token comment" spellcheck="true"># 关键点2：这里如果不写username的话，序列化器直接用的是模型类的username</span>
    <span class="token comment" spellcheck="true"># 这两者的区别在于，如果覆盖写了username，他表示的可以是任何前端传来的数据，如果是模型类绑定，那只能是用户名了</span>
    <span class="token comment" spellcheck="true"># 我们这里username用于多方登录的校验数据，必须要重写</span>
    <span class="token comment" spellcheck="true"># 而password不用重写，因为password用的就是模型类本身的</span>
    username <span class="token operator">=</span> serializers<span class="token punctuation">.</span>CharField<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">class</span> <span class="token class-name">Meta</span><span class="token punctuation">:</span>
        model <span class="token operator">=</span> models<span class="token punctuation">.</span>User
        fields <span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'username'</span><span class="token punctuation">,</span><span class="token string">'password'</span><span class="token punctuation">]</span>
    <span class="token keyword">def</span> <span class="token function">validate</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> attrs<span class="token punctuation">)</span><span class="token punctuation">:</span>
        username <span class="token operator">=</span> attrs<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'username'</span><span class="token punctuation">)</span>
        password <span class="token operator">=</span> attrs<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'password'</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> username<span class="token punctuation">:</span>
            <span class="token keyword">if</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span><span class="token string">'^1[3-9][0-9]{9}$'</span><span class="token punctuation">,</span> username<span class="token punctuation">)</span><span class="token punctuation">:</span>
                user <span class="token operator">=</span> models<span class="token punctuation">.</span>User<span class="token punctuation">.</span>objects<span class="token punctuation">.</span>filter<span class="token punctuation">(</span>mobile<span class="token operator">=</span>username<span class="token punctuation">)</span><span class="token punctuation">.</span>first<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">elif</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span><span class="token string">'^.+@.+$'</span><span class="token punctuation">,</span> username<span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment" spellcheck="true"># 邮箱</span>
                user <span class="token operator">=</span> models<span class="token punctuation">.</span>User<span class="token punctuation">.</span>objects<span class="token punctuation">.</span>filter<span class="token punctuation">(</span>email<span class="token operator">=</span>username<span class="token punctuation">)</span><span class="token punctuation">.</span>first<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                user <span class="token operator">=</span> models<span class="token punctuation">.</span>User<span class="token punctuation">.</span>objects<span class="token punctuation">.</span>filter<span class="token punctuation">(</span>username<span class="token operator">=</span>username<span class="token punctuation">)</span><span class="token punctuation">.</span>first<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">if</span> user<span class="token punctuation">:</span>
                <span class="token keyword">if</span> user<span class="token punctuation">.</span>check_password<span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">:</span>
                    <span class="token comment" spellcheck="true"># 关键点3：jwt_payload_handler把用户数据对象转化成用户信息的字典</span>
                    <span class="token comment" spellcheck="true"># jwt_encode_handler把用户信息的字典转化成token</span>
                    payload <span class="token operator">=</span> jwt_payload_handler<span class="token punctuation">(</span>user<span class="token punctuation">)</span>
                    <span class="token comment" spellcheck="true"># print('user:',user,type(user))</span>
                    token <span class="token operator">=</span> jwt_encode_handler<span class="token punctuation">(</span>payload<span class="token punctuation">)</span>
                    <span class="token comment" spellcheck="true"># print('payload:',payload,type(payload))</span>
                    <span class="token comment" spellcheck="true"># print('token:',token)</span>
                    <span class="token comment" spellcheck="true"># 关键点4：如果我们要给序列化器添加数据，让视图函数去使用</span>
                    <span class="token comment" spellcheck="true"># 通常都是传给对象的context属性,当然直接赋值也可以，这只是他给我们提供的传值接口</span>
                    self<span class="token punctuation">.</span>context<span class="token punctuation">[</span><span class="token string">'token'</span><span class="token punctuation">]</span> <span class="token operator">=</span> token
                    self<span class="token punctuation">.</span>context<span class="token punctuation">[</span><span class="token string">'user'</span><span class="token punctuation">]</span> <span class="token operator">=</span> user
                    self<span class="token punctuation">.</span>user <span class="token operator">=</span> user
                    <span class="token keyword">return</span> attrs
                <span class="token keyword">else</span><span class="token punctuation">:</span>
                    <span class="token keyword">raise</span> ValidationError<span class="token punctuation">(</span><span class="token string">'密码错误'</span><span class="token punctuation">)</span>
            <span class="token keyword">raise</span> ValidationError<span class="token punctuation">(</span><span class="token string">'不存在用户'</span><span class="token punctuation">)</span>
        <span class="token keyword">raise</span> ValidationError<span class="token punctuation">(</span><span class="token string">'请输入用户名'</span><span class="token punctuation">)</span>

</code></pre>
<p><strong>views</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">LoginApi</span><span class="token punctuation">(</span>ViewSet<span class="token punctuation">)</span><span class="token punctuation">:</span>
    authentication_classes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> request<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true"># 在调用序列化类给context传数据，可以直接在序列化类中调用</span>
        <span class="token comment" spellcheck="true"># 关键点1：注意区分序列化传值与反序列化</span>
        <span class="token comment" spellcheck="true"># 这里只要拿字典取校验数据，那就传给data</span>
        <span class="token comment" spellcheck="true"># 如果是要把数据对象转化成字典就传给instance</span>
        user_ser <span class="token operator">=</span> sers<span class="token punctuation">.</span>LoginSer<span class="token punctuation">(</span>data<span class="token operator">=</span>request<span class="token punctuation">.</span>data<span class="token punctuation">,</span> context<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'request'</span><span class="token punctuation">:</span> request<span class="token punctuation">}</span><span class="token punctuation">)</span>
        user_ser<span class="token punctuation">.</span>is_valid<span class="token punctuation">(</span>raise_exception<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        token <span class="token operator">=</span> user_ser<span class="token punctuation">.</span>context<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'token'</span><span class="token punctuation">)</span>
        user <span class="token operator">=</span> user_ser<span class="token punctuation">.</span>context<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'user'</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>user_ser<span class="token punctuation">.</span>user<span class="token punctuation">)</span>
        <span class="token keyword">return</span> Response<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'code'</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">'msg'</span><span class="token punctuation">:</span> <span class="token string">'登录成功'</span><span class="token punctuation">,</span> <span class="token string">'token'</span><span class="token punctuation">:</span> token<span class="token punctuation">,</span> <span class="token string">'username'</span><span class="token punctuation">:</span> user<span class="token punctuation">.</span>username<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="打赏" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">点我就给你看点有意思的</div><div id="qr" style="display:none;"><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png" alt="支付宝" title="支付宝"></a><div><span style="color:#00A3EE"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-alipay-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg" alt="QQ 支付" title="QQ 支付"></a><div><span style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg" alt="微信支付" title="微信支付"></a><div><span style="color:#2DC100"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-pay-line"></use></svg></span></div></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong>爱肖彤真是太好了</li><li class="post-copyright-link"><strong>本文链接：</strong><a href="http://huang_zhao.gitee.io/task/2020/02/15/python/%E6%A1%86%E6%9E%B6/drf%E2%98%9Ejwt%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%8F%91%E4%B8%8E%E6%89%8B%E5%8A%A8%E7%AD%BE%E5%8F%91/" title="drf☞jwt自动签发与手动签发">http://huang_zhao.gitee.io/task/2020/02/15/python/%E6%A1%86%E6%9E%B6/drf%E2%98%9Ejwt%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%8F%91%E4%B8%8E%E6%89%8B%E5%8A%A8%E7%AD%BE%E5%8F%91/</a></li><li class="post-copyright-license"><strong>版权声明：</strong>本博客所有文章除特别声明外，均默认采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> 许可协议。</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/task/2020/02/15/python/%E6%A1%86%E6%9E%B6/drf%E5%86%85%E7%BD%AE%E6%8E%92%E5%BA%8F%E6%BA%90%E7%A0%81/" rel="prev" title="drf内置排序源码"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">drf内置排序源码</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/task/2020/01/25/python/%E5%AD%A6%E4%B9%A0/python%20%E9%AD%94%E6%B3%95%E6%96%B9%E6%B3%95%E6%80%BB%E7%BB%93/" rel="next" title="魔法方法"><span class="post-nav-text">魔法方法</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> 爱肖彤真是太好了</span></div><div class="powered"><span>由 <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> 驱动 v5.1.1</span><span class="footer-separator">|</span><span>主题 - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v0.9.7</span></div><div class="live_time"><span>本博客已萌萌哒地运行</span><span id="display_live_time"></span><span class="moe-text">(●'◡'●)</span><script>function blog_live_time() {
  window.setTimeout(blog_live_time, 1000);
  const start = new Date('2019-04-12T00:00:00');
  const now = new Date();
  const timeDiff = (now.getTime() - start.getTime());
  const msPerMinute = 60 * 1000;
  const msPerHour = 60 * msPerMinute;
  const msPerDay = 24 * msPerHour;
  const passDay = Math.floor(timeDiff / msPerDay);
  const passHour = Math.floor((timeDiff % msPerDay) / 60 / 60 / 1000);
  const passMinute = Math.floor((timeDiff % msPerHour) / 60 / 1000);
  const passSecond = Math.floor((timeDiff % msPerMinute) / 1000);
  display_live_time.innerHTML = " " + passDay + " 天 " + passHour + " 小时 " + passMinute + " 分 " + passSecond + " 秒";
}
blog_live_time();
</script></div><div id="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_site_uv" title="总访客量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-user-line"></use></svg></span><span id="busuanzi_value_site_uv"></span></span><span class="footer-separator">|</span><span id="busuanzi_container_site_pv" title="总访问量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg></span><span id="busuanzi_value_site_pv"></span></span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a><a class="popup-trigger hty-icon-button icon-search" id="search" href="javascript:;" title="搜索"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-search-line"></use></svg></span></a><script>window.addEventListener("DOMContentLoaded", () => {
  // Handle and trigger popup window
  document.querySelector(".popup-trigger").addEventListener("click", () => {
    document.querySelector(".popup").classList.add("show");
    setTimeout(() => {
      document.querySelector(".search-input").focus();
    }, 100);
  });

  // Monitor main search box
  const onPopupClose = () => {
    document.querySelector(".popup").classList.remove("show");
  };

  document.querySelector(".popup-btn-close").addEventListener("click", () => {
    onPopupClose();
  });

  window.addEventListener("keyup", event => {
    if (event.key === "Escape") {
      onPopupClose();
    }
  });
});
</script><script src="/task/js/search/local-search.js" defer></script><div class="popup search-popup"><div class="search-header"><span class="popup-btn-close close-icon hty-icon-button"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-close-line"></use></svg></span></div><div class="search-input-container"><input class="search-input" id="local-search-input" type="text" placeholder="搜索..." value=""></div><div id="local-search-result"></div></div></div><script defer src="/task/js/utils.js"></script><script defer src="/task/js/hexo-theme-yun.js"></script></body></html>